/*
 * @Author: your name
 * @Date: 2022-03-21 10:18:17
 * @LastEditTime: 2022-03-25 18:18:05
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \mallAdmin\src\app\main\recharge\order\order-detail\order-detail.component.ts
 */
import { Component, OnInit, ViewChild } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { ActivatedRoute } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { CommonService } from 'src/app/services-common/common.service';
import { RechargeOrderService } from '../recharge-order.service';

@Component({
  selector: 'app-order-detail',
  templateUrl: './order-detail.component.html',
  styleUrls: ['./order-detail.component.scss']
})
export class OrderDetailComponent implements OnInit {
  @ViewChild('rechargeFollowRef') rechargeFollowRef: any;

  routeParams: any = {};

  followTableParams: any = {
    tableData: [],
    loading: false,
    page: 1,
    pageSize: 10,
    total: 0
  };

  detailsData: any = {}; // 详情Data

  // 跟进Params
  followParams: any = {};
  
  // 售后退款 Modal
  refundValidateForm!: FormGroup;
  isRefundVisible: boolean = false;
  submitLoading: boolean = false;

  constructor(
    private message: NzMessageService,
    private fb: FormBuilder,
    private activatedRoute: ActivatedRoute,
    private commonService: CommonService,
    private services: RechargeOrderService
  ) {
    activatedRoute.queryParams.subscribe((res: any) => {
      this.routeParams = res || {};
    })
  }

  ngOnInit(): void {
    this.refundValidateForm = this.fb.group({
      refundPrice: ['', [Validators.required]],
      note: ['']
    });
    this.loadDeatilsData();
    this.loadFollowTable();
  }

  // 查询详情
  async loadDeatilsData() {
    const res: any = await this.services.queryRechargeOrderDetail({id: this.routeParams.id});
    if (res.code === 0) {
      const { data } = res || {};
      this.detailsData = data || {};
    } else {
      this.message.error(res.message);
    }
  }

  // 查询跟进Table
  async loadFollowTable() {
    let params = {
      type: 2,
      businessNo: this.routeParams.businessNo
    }
    this.followTableParams.loading = true;
    const res: any = await this.services.queryRechargeFollowList(params);
    this.followTableParams.loading = false;
    
    if (res.code === 0) {
      const { records } = res.data || {};
      records.map((item: any) => {
        item.enclosure = (item.enclosure && item.enclosure.length) ? item.enclosure.split(',') : [];
      })
      this.followTableParams.tableData = records || [];
    } else {
      this.message.error(res.message);
    }
  }

  // 跟进操作
  followModal() {
    this.followParams = {
      type: 2,
      businessNo: this.routeParams.businessNo
    }
    this.rechargeFollowRef.isModelVisible = true;
  }

  showRefundModal() {
    this.refundValidateForm.reset();
    this.isRefundVisible = true;
  }
  
  // 售后退款
  async subRefunAmount() {
    if (this.refundValidateForm.valid) {
      let params = {
        ...this.refundValidateForm.value,
        id: this.routeParams.id
      }
      
      this.submitLoading = true;
      const res: any = await this.services.rechargeRefundData(params);
      this.submitLoading = false;
      
      if (res.code === 0) {
        this.message.success('操作成功');
        this.isRefundVisible = false;
        this.loadDeatilsData();
        this.loadFollowTable();
      } else {
        this.message.error(res.message);
      }
    } else {
      Object.values(this.refundValidateForm.controls).forEach(control => {
        if (control.invalid) {
          control.markAsDirty();
          control.updateValueAndValidity({ onlySelf: true });
        }
      });
    }
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.followTableParams.page = index;
    // this.loadTable();
  }
  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.followTableParams.page = 1;
    this.followTableParams.pageSize = sizeNum;
    // this.loadTable();
  }

  // 截取url
  interceptStr(str: string) {
    if (str) {
      str = str.substr(str.lastIndexOf('/') + 1)
    }
    return str || '-';
  }

  // 下载
  downFile(url: string) {
    window.open(url, '_blank');
  }
}
